import { Component, OnInit } from '@angular/core';
import {
  NavController,
  LoadingController
} from '@ionic/angular';
import { DataService } from '@services/data.service';
import { CacheService } from '@services/cache.service';
import { Router, ActivatedRoute } from '@angular/router';

import { DeviceControllerPage } from './device-controller/page';


@Component({
  selector: 'controller',
  templateUrl: 'page.html',
  styleUrls: ['style.scss']
})
export class ControllerPage implements OnInit {
  projectId;            // 项目ID
  where = null;
  state = 'group';     // 判断显示的控制页面
  groupParams;         // 获取群控数据
  onlyParams;         // 获取单控数据
  // 界面功能点
  functionList = [{ functionCode: 'group', functionName: '群控' }, { functionCode: 'only', functionName: '单控' }];

  constructor(
    private loadCtrl: LoadingController,
    private dataService: DataService,
    private navCtrl: NavController,
    private cache: CacheService,
    private activatedRoute: ActivatedRoute
  ) {
      this.activatedRoute.queryParams.subscribe(params => {
        this.projectId = params.projectId || this.cache.currentProject.projectId;
      });
  }

  ngOnInit() {
  }

  ionViewWillEnter() {
    this.getGroupParams();
    this.getOnlyParams();
  }


  changeAlarmTab(data) {
    this.state = data;
    if (data === 'group') {
      // this.getAlarm(this.projectId, null);
      this.getGroupParams();
    } else if (data === 'only') {
      this.getOnlyParams();
      // this.getFireParams(this.projectId, null, false);
    } else {
      // this.getEvent(this.projectId, null, null);
    }
  }


  // 点击显示设备
  enterItem(i, data) {
    console.log(i);
    if (!data.executing) {
      if (i === this.where) {
        this.groupParams[i].isShown = !this.groupParams[i].isShown;
      } else if (typeof (this.where) === 'number') {
        this.groupParams[this.where].isShown = false;
        this.groupParams[i].isShown = true;
      } else {
        this.groupParams[i].isShown = true;
      }
      this.where = i;
    }
  }

  // 单控点击跳转控制界面
  goDevice(item, i, data) {
    // this.navCtrl.push(DeviceControllerPage, {
    //   data: data.Infos,
    //   index: i,
    //   deviceName: data.name,

    // });
  }

  // 点击执行控制服务
  carryItem(i, data) {
    data.fail = false;
    data.successed = false;
    data.executing = true;
    this.dataService.getExecute(data.controlGroupId).subscribe(res => {
      console.log(res);
      if (res.success) {
        data.Finished = true;
        data.successed = true;
        data.executing = false;
        setTimeout(() => {
          data.successed = false;
        }, 3000);
      } else {
        data.Finished = true;
        data.executing = false;
        data.fail = true;
      }
    });
  }

  // 请求群控设备数据
  getGroupParams() {
    this.dataService.getCotrollers(this.projectId).subscribe(res => {
      console.log(res);
      this.groupParams = res.data;
    });
    // this.groupParams =
  }

  // 获取单控数据
  getOnlyParams() {
    this.dataService.getCotroller(this.projectId).subscribe(res => {
      const map = {};
      const arr = [];
      for (let i = 0; i < res.data.length; i++) {
        const ai = res.data[i];
        if (!map[ai.areaId]) {
          arr.push({
            areaId: ai.areaId,
            areaName: ai.areaName,
            deviceId: ai.deviceId,
            deviceTypeName: ai.deviceTypeName,
            deviceName: ai.deviceName,
            Infos: [ai]
          });
          map[ai.areaId] = ai;
        } else {
          for (let j = 0; j < arr.length; j++) {
            const dj = arr[j];
            if (dj.areaId === ai.areaId) {
              dj.Infos.push(ai);
              break;
            }
          }
        }
      }
      this.onlyParams = arr;
    });
  }

  changeStatu(item) {
    item.isOpen = !item.isOpen;
    item.fail = false;
    item.successed = false;
    item.executing = true;
    console.log(item);
    this.dataService.changeCotroller(item.deviceId, item.deviceParameterId, item.isOpen).subscribe(res => {
      console.log(res);
      if (res.success) {
        item.executing = false;
        item.successed = true;
        setTimeout(() => {
          item.successed = false;
        }, 3000);
      } else {
        item.fail = true;
        item.executing = false;
        item.isOpen = !item.isOpen;
      }
    });
  }
}
